package com.hspedu.spring.hspapplicationcontext;

import com.hspedu.spring.bean.*;
import com.hspedu.spring.component.UserAction;
import com.hspedu.spring.component.UserDao;
import com.hspedu.spring.component.UserService;
import com.hspedu.spring.controller.OrderAction;
import com.hspedu.spring.dao.OrderDao;
import com.hspedu.spring.depinjection.PhoneService;
import com.hspedu.spring.factory.MyStaticFactory;
import com.hspedu.spring.service.MemberServiceImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


import java.util.Arrays;

/**
 * @author Charlie
 * @version 1.0
 * @date 29/10/2025 下午10:00
 */
public class ApplicationContextTest {

    @Test
    public void setPropertiesByInjection() {
        ApplicationContext ioc=
                new ClassPathXmlApplicationContext("beans07.xml");
        System.out.println("ok");
        PhoneService phoneService = ioc.getBean("phoneService", PhoneService.class);
        phoneService.save();

    }


    @Test
    public void setPropertiesByAutowired() {
        ApplicationContext ioc=
                new ClassPathXmlApplicationContext("beans06.xml");


        UserService userService = ioc.getBean("userService", UserService.class);
        System.out.println("userService" +  userService);

        UserService userService200 = ioc.getBean("userService200", UserService.class);
        System.out.println("userService200" +  userService200);

        UserService userService500 = ioc.getBean("userService500", UserService.class);
        System.out.println("userService500" +  userService500);

        UserAction userAction = ioc.getBean("userAction", UserAction.class);
        System.out.println(userAction);

        userAction.sayOk();

    }

    @Test
    public void setBeanByAnnotation() {
        ApplicationContext ioc =
                new ClassPathXmlApplicationContext("beans05.xml");

        Object userDao = ioc.getBean(UserDao.class);

        System.out.println("orderDao" + userDao);

    }


    @Test
    public void setBeanByAutowire() {
        ApplicationContext ioc =
                new ClassPathXmlApplicationContext("beans03.xml");

        OrderAction orderAction = ioc.getBean("orderAction", OrderAction.class);
        System.out.println(orderAction.getOrderService().getOrderDao());

    }


    @Test
    public void getBeanByProperties() {
        ApplicationContext ioc =
                new ClassPathXmlApplicationContext("beans.xml");

        Monster monster999 =(Monster) ioc.getBean("monster999");
        System.out.println(monster999);

    }

    @Test
    public void testBeanPostProcessor(){
        ApplicationContext ioc =
                new ClassPathXmlApplicationContext("beans02.xml");
        House house = ioc.getBean("house", House.class);
        House house1 = ioc.getBean("house1", House.class);
        System.out.println("使用得house"+house);
        System.out.println("使用得house"+house1);

        ((ConfigurableApplicationContext)ioc).close();

    }

    @Test
    public void testBeanLifeStyle() {
        ApplicationContext ioc =
                new ClassPathXmlApplicationContext("beans.xml");
        House house = ioc.getBean("house", House.class);
        System.out.println("使用得house"+house);

        ((ConfigurableApplicationContext)ioc).close();

    }


    @Test
    public void setBeanByRef(){


       /* MemberServiceImpl memberService =
                ioc.getBean("memberService", MemberServiceImpl.class);

        memberService.add();
*/


        //Monster monster06 = ioc.getBean("monster06",Monster.class);
       /* Monster monster01 = ioc.getBean("monster01", Monster.class);
        System.out.println(Arrays.toString(ioc.getBeanDefinitionNames()));
        System.out.println(monster01);*/
        /*System.out.println("classpath 根目录是否存在 beans.xml = " +
                getClass().getClassLoader().getResource("beans.xml"));
        System.out.println("所有 bean 定义 = " + Arrays.toString(ioc.getBeanDefinitionNames()));*/
    }


    //给集合数组属性赋值
    @Test
    public void setBeanByCollection() {
        ApplicationContext ioc =
                new ClassPathXmlApplicationContext("beans.xml");

        Master master = ioc.getBean("master", Master.class);
        System.out.println("master= " + master);
    }


    @Test
    public void setBeanByUtils() {
        ApplicationContext ioc =
                new ClassPathXmlApplicationContext("beans.xml");

        BookStore bookStore = ioc.getBean("bookStore", BookStore.class);
        System.out.println("bookstore= " + bookStore);
    }



    @Test
    public void setBeanByFactory() {
        ApplicationContext ioc =
                new ClassPathXmlApplicationContext("beans.xml");
        Monster my_monster01 = ioc.getBean("my_monster01", Monster.class);
        System.out.println("my_monster01=  " + my_monster01);
    }

    @Test
    public void setBeanByInstanceFactory() {
        ApplicationContext ioc =
                new ClassPathXmlApplicationContext("beans.xml");
        Monster my_monster01 = ioc.getBean("my_monster01", Monster.class);
        Monster my_monster02 = ioc.getBean("my_monster01", Monster.class);
        System.out.println("my_monster01=  " + my_monster01);

        System.out.println(my_monster01==my_monster02);
    }


    @Test
    public void setBeanByFactoryBean() {
        ApplicationContext ioc =
                new ClassPathXmlApplicationContext("beans.xml");
        Monster my_monster05 = ioc.getBean("my_monster05", Monster.class);
        Monster my_monster04 = ioc.getBean("my_monster05", Monster.class);
        System.out.println("monster_05 =" + my_monster05);
        System.out.println(my_monster04==my_monster05);

    }

    @Test
    public void setBeanByFactoryBean1() {
        ApplicationContext ioc =
                new ClassPathXmlApplicationContext("beans.xml");
        Monster monster11 = ioc.getBean("monster11", Monster.class);

        System.out.println("monster11 =" + monster11);


    }

    @Test
    public void setBeanScope() {
        ApplicationContext ioc =
                new ClassPathXmlApplicationContext("beans.xml");
        Cat cat01 = ioc.getBean("cat01",Cat.class);
        Cat cat02 = ioc.getBean("cat01",Cat.class);
        Cat cat03 = ioc.getBean("cat01",Cat.class);
        Cat cat04 = ioc.getBean("cat01",Cat.class);

        System.out.println(cat01);
        System.out.println(cat02);
        System.out.println(cat03);
        System.out.println(cat04);


    }




}
